#include "histogram_dialog.h"
#include "ui_histogram_dialog.h"
#include <QGraphicsItem>
#include <algorithm>
#include <qmath.h>

Histogram_dialog::Histogram_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Histogram_dialog)
{
    ui->setupUi(this);

    graphScene = new QGraphicsScene();

    number_of_histogram = 0;

    ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

Histogram_dialog::~Histogram_dialog()
{    
    delete ui;
    delete graphScene;
}

void Histogram_dialog::AddHistogram(std::vector<double> hist_values, QString title)
{
    double max = fabs(*(std::max_element(hist_values.begin(), hist_values.end())));
    double min = fabs(*(std::max_element(hist_values.begin(), hist_values.end())));
    double range_limit = ((max > min) ? max : min);

    // scale gradations
    if(range_limit == 0 || range_limit < 0.5) range_limit = 0.5;
    else if(range_limit < 1) range_limit = 1;
        else if(range_limit < 100) range_limit = 100;
            else if(range_limit < 1000) range_limit = 1000;

    const double k = 50/ range_limit;

    const int offset = 30; // offset between histograms

    float posY = number_of_histogram * (100 + offset);

    // draw histogram axis
    float axe_X = 0;
    QGraphicsLineItem* l1 = new QGraphicsLineItem(axe_X, posY + 50, axe_X, posY - 50);
    graphScene->addItem(l1);

    // draw markers of axe
    float marker_Y = posY - 50;
    float marker_Value = range_limit;
    for(int i = 0; i < 5; i++)
    {
        int len;
        if(i%2 == 0) len = 5;
            else len = 3;

        QGraphicsLineItem* l1 = new QGraphicsLineItem(axe_X - len, marker_Y, axe_X + len, marker_Y);
        graphScene->addItem(l1);

        QString text;
        text.sprintf("  %.2f", marker_Value);
        QGraphicsTextItem* textItem = new QGraphicsTextItem(text);
        textItem->setPos(axe_X - len - 40, marker_Y - 10);
        graphScene->addItem(textItem);

        marker_Y += 25;
        marker_Value -= range_limit/2.0;
    }

    // draw histogram rects
    for(int i = 0; i < hist_values.size(); i++)
    {
        float value = hist_values[i];
        QRectF r1(i*10 + 0.0, posY, 10.0, -value * k);
        QGraphicsRectItem* rect1 = new QGraphicsRectItem(r1);
        rect1->setBrush(QBrush(QColor(0, 0, 0, 150)));
        rect1->setPen(QPen(Qt::black, 0.5));
        graphScene->addItem(rect1);
    }

    // histogram title
    QString range_text;
    range_text.sprintf("\n value limits = (-%.2f, %.2f)", range_limit, range_limit);
    QGraphicsTextItem* text = new QGraphicsTextItem(title + range_text);
    text->setPos((hist_values.size()+5)*10, posY);
    text->setFont(QFont("Arial", 10));

    graphScene->addItem(text);

    graphScene->setBackgroundBrush(QBrush(QColor(200, 200, 200)));

    ui->graphicsView->setScene(graphScene);

    number_of_histogram++;
}

void Histogram_dialog::Clear()
{
    graphScene->clear();
}

void Histogram_dialog::on_Plus_pushButton_clicked()
{
    ui->graphicsView->scale(2.0, 2.0);
}

void Histogram_dialog::on_Minus_pushButton_clicked()
{
    ui->graphicsView->scale(0.5, 0.5);
}
